﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework;

namespace RebEngine.Managers
{
    public class LogManager: GameComponent
    {
       
        static FileStream file;

        public LogManager(Game game):
            base(game)
        {
            string filename = Path.Combine(Directory.GetCurrentDirectory(), "logfile.txt");
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            file = File.Create(filename);
        }
        
        /// <summary>
        /// Writes a line to the log. Uses the current time to show when the different information occured. Can be used for error checking
        /// </summary>
        /// <param name="line">The Line to Write to the Log</param>
        public static void Write(string line)
        {
            string mins, secs;

            if (DateTime.Now.TimeOfDay.Minutes < 10)
                mins = "0" + DateTime.Now.TimeOfDay.Minutes.ToString();
            else
                mins = DateTime.Now.TimeOfDay.Minutes.ToString();

            if (DateTime.Now.TimeOfDay.Seconds < 10)
                secs = "0" + DateTime.Now.TimeOfDay.Seconds.ToString();
            else
                secs = DateTime.Now.TimeOfDay.Seconds.ToString();

            line = DateTime.Now.TimeOfDay.Hours.ToString() + ":" + mins
                + ":"+ secs + "  " + line;
            byte[] info = new UTF8Encoding(true).GetBytes(line);
            byte[] nextLine = new UTF8Encoding(true).GetBytes("\r\n");

            file.Write(info, 0, info.Length);
            file.Write(nextLine, 0, nextLine.Length);
        }



    }
}
